<?php

/**
 * @file
 * Definition of example_handler_custom_field
 */

/**
 * Provides a custom views field.
 */
class jessica_stylebook_handler_style_item_latest_article extends views_handler_field {
    function option_definition() {
        $options = parent::option_definition();
        return $options;
    }

    function options_form(&$form, &$form_state) {
        parent::options_form($form, $form_state);
    }

    function query() {
        // do nothing -- to override the parent query.
    }

    function render($data) {
        //dpm($data, 'latest data');

        $query = 'SELECT
            node_article.nid as nid,
            hex_colour.field_hex_colour_code_value as colour,
            image_file.uri as uri
        FROM
            {node} n
        LEFT JOIN
            {field_data_field_styling_item_id} ss_id
        ON
        (ss_id.field_styling_item_id_target_id = n.nid)
        LEFT JOIN
            {field_data_field_styling_item} si
        ON
            (si.field_styling_item_value = ss_id.entity_id)
        LEFT JOIN
            {node} node_article
        ON
            (node_article.nid = si.entity_id)
        LEFT JOIN
            field_data_field_image article_image
        ON
            (node_article.nid = article_image.entity_id)
        LEFT JOIN
            file_managed image_file
        ON
            (image_file.fid = article_image.field_image_fid)
        LEFT JOIN
            {field_data_field_styling_item} article_si
        ON
            (node_article.nid = article_si.entity_id)
        LEFT JOIN
            {field_data_field_pin_show_on_thumbnail} show_thumb
        ON
            (article_si.field_styling_item_value = show_thumb.entity_id)
        LEFT JOIN
            {field_data_field_styling_item_id} article_ss_id
        ON
            (article_si.field_styling_item_value = article_ss_id.entity_id)
        LEFT JOIN
            {node} si_node
        ON
            (article_ss_id.field_styling_item_id_target_id = si_node.nid)
        LEFT JOIN
            {field_data_field_item_colour} si_colour
        ON
            (si_node.nid = si_colour.entity_id)
        LEFT JOIN
            {taxonomy_term_data} tax
        ON
            (si_colour.field_item_colour_tid = tax.tid)
        LEFT JOIN
            {field_data_field_hex_colour_code} hex_colour
        ON
            (tax.tid = hex_colour.entity_id)
        WHERE
            n.type = :type
        AND
            n.nid = :nid
        AND
            node_article.status = 1
        AND
            si_node.status = 1
        AND
        	article_image.bundle = :bundle
        AND
            si_colour.delta = 0';

        $result = db_query($query, array(':type' => 'xenyo_sc_styling_item', ':nid' => $data->nid, ':bundle' => 'xenyo_sc_styling_article'));

        $articles = array();

        foreach ($result as $record) {
            $articles[$record->nid]['colour'][] = $record->colour;
            $articles[$record->nid]['uri'] = $record->uri;
        }

        $article_count = count($articles);

        $output = '<div class="item-style-section">';
        $output .= '<h4>Style: <span>' . $article_count . '</span></h4>';
        $i = 0;

        foreach ($articles as $key => $article) {
            $path = base_path() . drupal_get_path_alias('node/' . $key);

            $output .= '<div class="item-style">';
            $output .= '    <div class="field-image">';
            $output .= '        <a href="' . $path . '">';
            $output .= '            <img src="' . image_style_url('related_styling_article_thumbnail', $article['uri']) . '">';
            $output .= '        </a>';
            $output .= '    </div>';
            /*$output .= '    <div class="field-image-palette">';

            foreach($article['colour'] as $colour) {
                if ($colour != 'OTHER') {
                    $output .= '        <div style="background-color:' . $colour . ';"></div>';
                } else {
                    $output .= '        <div class="other-color"></div>';
                }
            }

            $output .= '    </div>';*/
            $output .= '</div>';

            //we only want the first 2 articles
            if($i == 1) {
                break;
            }

            $i++;
        }

        $output .= '</div>';

        return $output;
    }
}

